#include "queuethread.hpp"
#include <unistd.h>
#include <cstdio>
#include <time.h>
#include "Task.hpp"
#define PNUM 2
#define CNUM 2


int add(int x,int y)
{
    return x+y;
}
void* productor(void* args)
{
    QueueThread<task>* Thread=(QueueThread<task>*)args;
    while(true)
    {
        int x=rand()%1000;
        int y=rand()%100-x;
        task a(x,y,add);

        Thread->Push(a);
        sleep(1);
        std::cout <<pthread_self() <<" productor: "<< x << y <<std::endl;
    }
    return nullptr;
}

void* consumer(void* args)
{
    QueueThread<task>* Thread=(QueueThread<task>*)args;
    while(true)
    {
        //int x=rand()%1000;
        task a;
        Thread->Pop(&a);
        sleep(1);
        std::cout << pthread_self() <<" consumer: "<<std::endl;
    }
    return nullptr;
}

int main()
{

    srand(time(nullptr));
    pthread_t pid[PNUM],cid[CNUM];

    QueueThread<int>* thread = new QueueThread<int>();
    for(int i=0;i<PNUM;i++)
    {
        pthread_create(pid+i,nullptr,productor,(void*)thread);
    }

    for(int i=0;i<CNUM;i++)
    {
        pthread_create(cid+i,nullptr,consumer,(void*)thread);
    }

    for(int i=0;i<PNUM;i++)
    {
        pthread_join(pid[i],nullptr);
    }
    for(int i=0;i<CNUM;i++)
    {
        pthread_join(cid[i],nullptr);
    }
    delete thread;
    return 0;
}